<?php


// https://github.com/mvar/apache2-log-parser
// https://github.com/mvar/log-parser
// https://github.com/mvar/log-parser/blob/master/docs/custom_parser.md


require __DIR__ . '/vendor/autoload.php';

use MVar\Apache2LogParser\AccessLogParser;
use MVar\LogParser\LogIterator;
use MVar\LogParser\SimpleParser;  

// Access log format from your Apache configuration
// It can be any of predefined `AccessLogParser::FORMAT_*` constants or custom string
/*
 
 $parser = new AccessLogParser('%h %l %u %t "%r" %>s %O "%{Referer}i" "%{User-Agent}i"');

// String which you want to parse
$line = '66.249.78.230 - - [29/Dec/2013:16:07:58 +0200] "GET /my-page/ HTTP/1.1" 200 2490 "-" ' .
    '"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"';

// var_export($parser->parseLine($line));

*/
$file_name="/Users/mac/zwn/zwn_git/simple_shell_course/apache_log/zwnPython/log/m-access_log_2021-04-05";
$dirFile='/Users/mac/zwn/zwn_git/simple_shell_course/apache_log/zwnPython/log/';



/**


$parser = new AccessLogParser("%h %{HTTP_HOST}e %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\" %{UID}C %{SESSIONID}C");

// $parser = new AccessLogParser(AccessLogParser::FORMAT_COMMON);


// echo "<pre>";
// var_dump($parser);
// die;


foreach (new LogIterator($file_name, $parser) as $line => $data) {
	// echo "<pre>";
	// var_dump($data);
    printf("%s %s\n", $data['request']['method'], $data['request']['path']);
}

*/


$parser = new SimpleParser('/"(?<method>\S+)\s+(?<path>\S+)\s+(?<protocol>\S+)"\s+(?<response_code>\d+)/');

// foreach (new LogIterator($file_name, $parser) as $data) {
//     // var_export($data);
//     // echo "\n";
// }


$dir = opendir( $dirFile );
$file_list = '';
while( false != ( $file = readdir( $dir ) ) )
{
	if( ( $file != "." ) and ( $file != ".." ) )
	{
		echo $fileName = $dirFile.$file;
		echo "\r\n";
		foreach (new LogIterator($fileName, $parser) as $data) {
			var_export($data);
			echo "\r\n";
		}
		// $file_list .= '  '.$file;
	}
}
closedir( $dir );

// <!-- 能用 -->